package com.easyclearance.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuguobao.util.SingaporeIcaRegUtil;
import com.easyclearance.aliutil.ImgByOcr;
import com.easyclearance.baseutil.ByteByUrl;
import com.easyclearance.baseutil.FileControl;
import com.easyclearance.baseutil.MailUtil;
import com.easyclearance.baseutil.SqlInjectInterceptor;
import com.easyclearance.dao.MailInfoMapper;
import com.easyclearance.entity.EnumTravel;
import com.easyclearance.entity.LeaderInfo;
import com.easyclearance.entity.MailInfo;
import com.easyclearance.entity.Malaysiatravinfo;
import com.easyclearance.entity.PassportInfo;
import com.easyclearance.entity.SingaporeTravInfo;
import com.easyclearance.entity.ThailandTravInfo;
import com.easyclearance.entity.TravbaoOrder;
import com.easyclearance.service.TravInfoService;
import com.github.pagehelper.PageInfo;


@CrossOrigin
@RestController
@RequestMapping(value = "/travApi",method = RequestMethod.POST)
public class TravInfoController extends BaseController {

	private final Logger logger= Logger.getLogger(TravInfoController.class);
	@Autowired
	private ImgByOcr imgByOcr;
	@Autowired
	private ByteByUrl byteByUrl;
//	@Autowired
//	private EmailUtil emailUtil;
	@Autowired
	private MailUtil mailUtil;
	@Autowired
	private MailInfoMapper mailInfoMapper;
	@Autowired
	private TravInfoService travInfoService;
	@Autowired
	private SqlInjectInterceptor sqlInjectInterceptor;
	
	/**
	 * 获取领队信息
	 * @param paramMap
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "/getLeaderInfo")
	public Map<String, Object> getLeaderInfo(@RequestBody Map<String, Object> paramMap
			,HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
			Map<String, Object> leaderInfo = travInfoService.getLeaderInfo(paramMap);
			resultMap = BaseController.correctReturn(leaderInfo);
		} catch (Exception e) {
			// TODO: handle exception
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
		return resultMap;
	}
	
	  /**
     * 使用ocr识别护照信息页
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/getPassportInfoByOcr")
    public Map<String, Object> getPassportInfoByOcr(HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> map = new HashMap<>();

        JSONObject imgJson0 = null;
        InputStream in = null;
        try {
//            System.out.println("进入get方法！");

            String fileName = "f" + new Date().getTime() + "";

            MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
            MultipartFile multipartFile = req.getFile("file");

            String realPath = "/tmp";
            try {
                File dir = new File(realPath);
                if (!dir.exists()) {
                    dir.mkdir();
                }
                //存储文件
                File file = new File(realPath, fileName + ".jpg");
                multipartFile.transferTo(file);
                //读取文件
                String filePathString = realPath + "/" + fileName + ".jpg";
                //输入流转换
                in = new FileInputStream(new File(filePathString));
                //字节流
                String inputStream = byteByUrl.GetImageStr(in);
                //读取
                imgJson0 = imgByOcr.getImgJson0(inputStream);
                //获取
                if (imgJson0 == null) {
                    map.put("success", true);
                    map.put("message", "获取为null");
                    map.put("data", new HashMap<>());
                } else {
                    map.put("success", true);
                    map.put("data", imgJson0);
                    map.put("message", "获取成功");
                }
                in.close();
                FileControl.deleteFile(filePathString);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("", e);
                map.put("success", false);
                map.put("message", "获取失败");
                map.put("data", new HashMap<>());
            } catch (IllegalStateException e) {
                e.printStackTrace();
                logger.error("", e);
                map.put("success", false);
                map.put("message", "获取失败");
                map.put("data", new HashMap<>());
            }
        } catch (Exception e) {
            logger.error("", e);
            map.put("success", false);
            map.put("message", "获取失败");
            map.put("data", new HashMap<>());
        }
        return map;
    }
    
	/**
	 * 提交护照信息
	 * @param paramMap
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "/createPassportInfo")
	public Map<String, Object> createPassportInfo(@RequestBody Map<String, Object> paramMap
			,HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
			Map<String, Object> createOrderInfo = travInfoService.createOrderInfo(paramMap);
			resultMap = BaseController.correctReturn(createOrderInfo);
		} catch (Exception e) {
			// TODO: handle exception
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
		return resultMap;
	}
	
	/**
	 * 获取订单列表
	 * @param paramMap
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "/getOrderList")
	public Map<String, Object> getOrderList(@RequestBody Map<String, Object> paramMap
			,HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
			Map<String, Object> orderlist = travInfoService.getOrderlist(paramMap);
			resultMap = BaseController.correctReturn(orderlist);
		} catch (Exception e) {
			// TODO: handle exception
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
		return resultMap;
	}
	
	/**
	 * 获取订单基本信息
	 * @param paramMap
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "/getOrderBaseInfo")
	public Map<String, Object> getOrderBaseInfo(@RequestBody Map<String, Object> paramMap
			,HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
			Map<String, Object> orderBaseInfo = travInfoService.getOrderBaseInfo(paramMap);
			resultMap = BaseController.correctReturn(orderBaseInfo);
		} catch (Exception e) {
			// TODO: handle exception
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
		return resultMap;
	}
	
	   
    @ResponseBody
    @PostMapping(value = "/writeBackInfo")
    public Map<String, Object> writeBackInfo(@RequestBody JSONObject jsonObject,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, jsonObject);
    		final Integer integer = jsonObject.getInteger("total");
    		Timer timer = new Timer();
    		timer.schedule(new TimerTask() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					travInfoService.loopAcquisitionMailContent(integer);
				}
			}, 1000,5 * 60 * 1000);
			resultMap = BaseController.correctReturn(new JSONObject());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * 读取邮箱内容并回写
     * @param jsonObject
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/writeBackOnceInfo")
    public Map<String, Object> writeBackOnceInfo(@RequestBody JSONObject jsonObject,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, jsonObject);
  
            // 创建一个实现Callable的任务
    		final Integer integer = jsonObject.getInteger("total");
            
         // 开启线程
            ExecutorService cacheThreadPool = Executors.newCachedThreadPool();
            Callable<Boolean> clientAction = new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                	travInfoService.loopAcquisitionMailContent(integer);
                    return true;
                }
            };
            try {
                cacheThreadPool.submit(clientAction);
            } catch (Exception e) {
                logger.error("执行clientAction失败:" + e.getMessage(), e);
            } finally {
                cacheThreadPool.shutdown();
            }
     
            // 当异步任务完成时，打印结果
//            future.thenAccept(System.out::println).join(); // 使用thenAccept来接收结果，并执行操作，join确保主线程等待
     
			resultMap = BaseController.correctReturn(new JSONObject());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.exceptionReturn();
		}
    	return resultMap;
    }
    
    /**
     * 读取邮箱
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/readUnreadMailsContent")
    public Map<String, Object> readUnreadMailsContent(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, new Object());
    		JSONArray linkedList = new JSONArray();
    		List<MailInfo> mailList = mailInfoMapper.selectList(new QueryWrapper<MailInfo>().eq("emailStatus", 2));
    		for(int i=0;i<mailList.size();i++) {
    			MailInfo mailInfo = mailList.get(i);
        		List<Map<String, Object>> readUnreadMailsContent = mailUtil.readUnreadMailsContent(mailInfo,20);
        		JSONArray parseArray = JSONArray.parseArray(JSON.toJSONString(readUnreadMailsContent));
        		linkedList.addAll(parseArray);
    		}
    		JSONObject jsonObject = new JSONObject();
    		jsonObject.put("info", linkedList);

			resultMap = BaseController.correctReturn(jsonObject);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.exceptionReturn();
		}
    	return resultMap;
    }
    
    /**
     * @ 创建旅行信息
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/createTravInfo")
    public Map<String, Object> createTravInfo(@RequestBody Map<String, Object> paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
    		//获取文件
    		Map<String, Object> createTravInfo = travInfoService.createTravInfo(paramMap);
			resultMap = BaseController.correctReturn(createTravInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @ 创建旅行信息
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/uploadPassport")
    public Map<String, Object> uploadPassport(@RequestParam("fileInfo") MultipartFile file,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
    		//获取文件
    		if (Objects.nonNull(file)) {
	    		List<PassportInfo> uploadExcelFile = travInfoService.uploadExcelFile(file);
	    		//
	    		Map<String, Object> hashMap = new HashMap<>();
	    		hashMap.put("personNum", uploadExcelFile.size());
	    		hashMap.put("personInfo", uploadExcelFile);
				resultMap = BaseController.correctReturn(hashMap);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    /**
     * @创建领队信息
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/createLeaderInfo")
    public Map<String, Object> createLeaderInfo(@RequestBody Map<String, Object> paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
    		LeaderInfo createLeaderInfo = travInfoService.createLeaderInfo(paramMap);
			resultMap = BaseController.correctReturn(createLeaderInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     *@ 添加旅行码
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/addTraInfocode")
    public Map<String, Object> addTraInfocode(@RequestBody Map<String, Object> paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
    		Map<String, Object> addTraInfocode = travInfoService.addTraInfocode(paramMap);
			resultMap = BaseController.correctReturn(addTraInfocode);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @添加旅行码
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/submit")
    public Map<String, Object> submit(@RequestBody Map<String, Object> paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
            // 开启线程
            ExecutorService cacheThreadPool = Executors.newCachedThreadPool();
            Callable<Boolean> clientAction = new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
            		travInfoService.submitAgain(paramMap);
                    return true;
                }
            };
            try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
                cacheThreadPool.submit(clientAction);
            } catch (Exception e) {
                logger.error("执行clientAction失败:" + e.getMessage(), e);
            } finally {
                cacheThreadPool.shutdown();
            }
            resultMap = BaseController.otherResultReturn(0, "重发成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @添加旅行码
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/sendMail")
    public Map<String, Object> sendMail(@RequestBody Map<String, Object> paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
            // 开启线程
            String status = paramMap.get("status").toString();
            String string = paramMap.get("content").toString();
            List<MailInfo> selectList = mailInfoMapper.selectList(new QueryWrapper<MailInfo>().eq("email_status", status));
            Collections.shuffle(selectList);
            mailUtil.sendEmail(string, selectList.get(0));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @獲取形成對應的護照信息
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/getPassportInfoByGroupCode")
    public Map<String, Object> getPassportInfoByGroupCode(@RequestBody Map<String, Object> paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
    		Map<String, Object> passportInfoByGroupCode = travInfoService.getPassportInfoByGroupCode(paramMap);
            resultMap = BaseController.correctReturn(passportInfoByGroupCode);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @獲取形成對應的行程信息
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/getTravelInfoByGroupCode")
    public Map<String, Object> getTravelInfoByGroupCode(@RequestBody Map<String, Object> paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
    		Map<String, Object> travelInfoByGroupCode = travInfoService.getTravelInfoByGroupCode(paramMap);
            resultMap = BaseController.correctReturn(travelInfoByGroupCode);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @修改護照信息
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/updatePassportInfo")
    public Map<String, Object> updatePassportInfo(@RequestBody JSONArray paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
    		List<PassportInfo> updatePassportInfo = travInfoService.updatePassportInfo(paramMap);
            resultMap = BaseController.correctReturn(updatePassportInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @修改馬來西亞信息
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/updateMalaTravelInfo")
    public Map<String, Object> updateMalaTravelInfo(@RequestBody JSONObject paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
    		Malaysiatravinfo updateMalaTravelInfo = travInfoService.updateMalaTravelInfo(paramMap);
            resultMap = BaseController.correctReturn(updateMalaTravelInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @修改新加坡
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/updateSingTravelInfo")
    public Map<String, Object> updateSingTravelInfo(@RequestBody JSONObject paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
    		SingaporeTravInfo updateSingTravelInfo = travInfoService.updateSingTravelInfo(paramMap);
            resultMap = BaseController.correctReturn(updateSingTravelInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @修改泰国
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/updateThailandTravelInfo")
    public Map<String, Object> updateThailandTravelInfo(@RequestBody JSONObject paramMap,
    		HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paramMap);
    		ThailandTravInfo thailandTravInfo = travInfoService.updateThailandTravInfo(paramMap);
            resultMap = BaseController.correctReturn(thailandTravInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1,e.getMessage());
		}
    	return resultMap;
    }
    
    @ResponseBody
    @PostMapping(value = "/getLeaderInfoList")
    public Map<String, Object> getLeaderInfoList(@RequestBody Map<String, Object> paraMap,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paraMap);
			PageInfo<LeaderInfo> leaderInfoList = travInfoService.getLeaderInfoList(paraMap);
			resultMap = BaseController.correctReturn(leaderInfoList);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    @ResponseBody
    @PostMapping(value = "/getEnumTravelList")
    public Map<String, Object> getEnumTravelList(@RequestBody Map<String, Object> paraMap,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paraMap);
			List<EnumTravel> enumTravelList = travInfoService.getEnumTravelList(paraMap);
			resultMap = BaseController.correctReturn(enumTravelList);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    @ResponseBody
    @PostMapping(value = "/getRecentOrders")
    public Map<String, Object> getRecentOrders(@RequestBody Map<String, Object> paraMap,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paraMap);
			Map<String, Object> recentOrders = travInfoService.getRecentOrders(paraMap);
			resultMap = BaseController.correctReturn(recentOrders);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    @ResponseBody
    @PostMapping(value = "/loginInfo")
    public Map<String, Object> loginInfo(@RequestBody Map<String, Object> paraMap,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paraMap);
			Map<String, Object> recentOrders = travInfoService.loginInfo(paraMap);
			resultMap = BaseController.correctReturn(recentOrders);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    @ResponseBody
    @PostMapping(value = "/getCountryPriceInfo")
    public Map<String, Object> getCountryPriceInfo(@RequestBody Map<String, Object> paraMap,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paraMap);
			Map<String, Object> countryPriceInfo = travInfoService.getCountryPriceInfo(paraMap);
			resultMap = BaseController.correctReturn(countryPriceInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    @ResponseBody
    @PostMapping(value = "/getTravelInfoList")
    public Map<String, Object> getTravelInfoList(@RequestBody Map<String, Object> paraMap, HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paraMap);
			Map<String, Object> travelInfoList = travInfoService.getTravelInfoList(paraMap);
			resultMap = BaseController.correctReturn(travelInfoList);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    @ResponseBody
    @PostMapping(value = "/updateOrderStatusInfo")
    public Map<String, Object> updateOrderStatusInfo(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, new Object());
			travInfoService.updateOrderStatusInfo();
			resultMap = BaseController.correctReturn(new HashMap<>());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    @ResponseBody
    @PostMapping(value = "/submitInformationtoMalaysia")
    public Map<String, Object> submitInformationtoMalaysia(@RequestBody Map<String, Object> paraMap,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paraMap);
			
			Map<String, Object> submitInformationtoMalaysia = travInfoService.submitInformationtoMalaysia(paraMap);
			resultMap = BaseController.correctReturn(submitInformationtoMalaysia);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @获取领队信息列表
     * @param paraMap
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/getPageLeaderInfo")
    public Map<String, Object> getPageLeaderInfo(@RequestBody Map<String, Object> paraMap,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paraMap);
			PageInfo<LeaderInfo> pageLeaderInfo = travInfoService.getPageLeaderInfo(paraMap);
			//转换对象
			Page<LeaderInfo> page = new Page<>();
			page.setRecords(pageLeaderInfo.getList());
			page.setTotal(pageLeaderInfo.getTotal());
			resultMap = BaseController.correctReturn(page);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @个人信息提交
     * @param paraMap
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/submitInforForPerson")
    public Map<String, Object> submitInforForPerson(@RequestBody Map<String, Object> paraMap,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			sqlInjectInterceptor.preHandle(request, response, paraMap);
			Map<String, Object> submitInforForPerson = travInfoService.submitInforForPerson(paraMap);
			resultMap = BaseController.correctReturn(submitInforForPerson);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    
    /**
     * @个人信息提交
     * @param paraMap
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/reSendSingapore")
    public Map<String, Object> reSendSingapore(@RequestBody Map<String, String>bodyMap,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {

    		String chromeDriver = "C:\\Program Files\\chromedriver\\chromedriver.exe";
            String tempOcrDictionary = "C:\\temp";
            String chromeCacheDictionary = "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\Default";
            bodyMap.put("healthDeclaration3","NO");
            //判断酒店类型
            if (bodyMap.containsKey("hotelName")) {
				String hotelName = String.valueOf(bodyMap.get("hotelName"));
				if (Objects.equals(hotelName.toUpperCase(), "OTHERS")) {
					bodyMap.put("accommodationType","O");
					bodyMap.put("otherAccommodation","DAY_TRIP");
				}else {
					bodyMap.put("accommodationType","H");
				}
			}
            logger.error("before singapore request："+JSON.toJSONString(bodyMap));
            String[] register = SingaporeIcaRegUtil.register(chromeDriver, tempOcrDictionary, chromeCacheDictionary, bodyMap, false);
            for (int i = 0; i < register.length; i++) {
            	resultMap.put(i+"", register[i]);
				logger.error("----------"+register[i]);
			}
			resultMap = BaseController.correctReturn(resultMap);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(1, e.getMessage());
		}
    	return resultMap;
    }
    
    
    /**
     * @写入出国宝相关数据
     * @param paraMap
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/writeTravbaoSingaporeOrder")
    public Map<String, Object> writeTravbaoSingaporeOrder(@RequestBody Map<String, String>bodyMap,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
    		//获取数据
    		String trav_id = bodyMap.get("travId").toString();
    		String entry_date = bodyMap.get("entryDate").toString();
    		String passport_num = bodyMap.get("passportNum").toString();
    		//首先判断是否插入过数据了
    		boolean istherearelatedorder = travInfoService.istherearelatedorder(trav_id, passport_num, entry_date);
    		if (!istherearelatedorder) {
				TravbaoOrder travbaoOrder = JSONObject.parseObject(JSON.toJSONString(bodyMap),TravbaoOrder.class);
				if (Objects.nonNull(travbaoOrder)) {
					travInfoService.insertTravbaoOrder(travbaoOrder);
					resultMap = BaseController.correctReturn(travbaoOrder);
				}else {
					resultMap = BaseController.otherResultReturn(2, "该订单数据转换失败");
				}
			}else {
				resultMap = BaseController.otherResultReturn(1, "该订单数据已经存在");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(-1, e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @循环获取没有发起请求的出国宝新加披订单数据
     * @param paraMap
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/cycleTravbaoSingaporeOrderData")
    public Map<String, Object> cycleTravbaoSingaporeOrderData(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			TimerTask timerTask = new TimerTask() {
	            @Override
	            public void run() {
	                // 获取没有执行的订单数据
	                travInfoService.initialSingaporeRequest();
	            }
	        };
            Timer timer = new Timer();
     
            long delay = 0; // 任务执行前的延迟时间（单位：毫秒）
            long period = 5 * 60 * 1000; // 定时任务的间隔时间（单位：毫秒）
     
            // 安排在指定的时间间隔重复执行任务
            timer.scheduleAtFixedRate(timerTask, delay, period);
            //返回
            resultMap = BaseController.otherResultReturn(0, "定时任务发起成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(-1, e.getMessage());
		}
    	return resultMap;
    }
    
    /**
     * @method 获取旅行信息
     * @param paraMap
     * @param response
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/selectTravelitineraryInfo")
    public Map<String, Object> selectTravelitineraryInfo(@RequestBody Map<String, Object> map,HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<>();
    	try {
			//获取旅行信息
    		if (map.containsKey("travId")) {
				String travId = map.get("travId").toString();
				Map<String, Object> selectTravelitineraryInfo = travInfoService.selectTravelitineraryInfo(travId);
				resultMap = BaseController.correctReturn(selectTravelitineraryInfo);
			}else {
	            //返回
	            resultMap = BaseController.otherResultReturn(1, "缺少关键参数");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resultMap = BaseController.otherResultReturn(-1, e.getMessage());
		}
    	return resultMap;
    }
}
